﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml.Linq;
using Zero.UControl.Demo.Views.UAdorner.Component;
using static PdfSharp.Capabilities;

namespace Zero.UControl.Demo.Views.UAdorner
{
    /// <summary>
    /// AdornerView.xaml 的交互逻辑
    /// </summary>
    public partial class AdornerView : Window
    {
        public AdornerView()
        {
            InitializeComponent();
            Loaded += AdornerView_Loaded;
        }

        #region 事件
        private void AdornerView_Loaded(object sender, RoutedEventArgs e)
        {
            Border border1 = new Border()
            {
                Padding = new Thickness(10),
                Background = new SolidColorBrush(Colors.White)
            };
            Label label1 = new Label()
            {
                Content = "WPF",
                FontSize = 12,
                FontWeight = FontWeights.Bold,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                Width = 120,
                Height = 80,
                Background = Brushes.LightGreen
            };
            border1.Child = label1;
            border1.Focusable = true;
            border1.GotFocus += Border_GotFocus;
            border1.LostFocus += Border_LostFocus;

            Border border2 = new Border()
            {
                Padding = new Thickness(10),
                Background = new SolidColorBrush(Colors.White)
            };
            Label label2 = new Label()
            {
                Content = "WPF",
                FontSize = 12,
                FontWeight = FontWeights.Bold,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                Width = 120,
                Height = 80,
                Background = Brushes.LightPink
            };
            border2.Child = label2;
            border2.Focusable = true;
            border2.GotFocus += Border_GotFocus;
            border2.LostFocus += Border_LostFocus;

            Canvas.SetLeft(border1, 100);
            Canvas.SetTop(border1, 100);
            Canvas.SetLeft(border2, 400);
            Canvas.SetTop(border2, 100);

            cvs.Children.Add(border1);
            cvs.Children.Add(border2);

            IElementDecorator[] decorators1 =
            {
                new SelectionAdorner(border1),
                new DraggableAdorner(border1),
                new TransformAdorner(border1)
            };
            IElementDecorator[] decorators2 =
            {
                new SelectionAdorner(border2),
                new DraggableAdorner(border2),
                new TransformAdorner(border2)
            };
            ElementDecoratorManager.ApplyAll(decorators1, border1, cvs);
            ElementDecoratorManager.HideAll(border1);
            ElementDecoratorManager.ApplyAll(decorators2, border2, cvs);
            ElementDecoratorManager.HideAll(border2);

            CanvasInit();
            RectInit();
        }
        #endregion

        #region 边框绘制
        private void CanvasInit()
        {
            cvs.MouseLeftButtonDown += Cvs_MouseLeftButtonDown;
            cvs.MouseMove += Cvs_MouseMove;
            cvs.MouseLeftButtonUp += Cvs_MouseLeftButtonUp;
        }
        bool isSelecting = false;
        private void Cvs_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point point = e.GetPosition(cvs);
            isSelecting = true;
            Canvas.SetLeft(SelectionRect, point.X);
            Canvas.SetTop(SelectionRect, point.Y);
            SelectionRect.Width = 0;
            SelectionRect.Height = 0;
        }

        private void Cvs_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isSelecting) return;
            Point startPoint = new Point(Canvas.GetLeft(SelectionRect), Canvas.GetTop(SelectionRect));
            Point point = e.GetPosition(cvs);

            double x = Math.Min(startPoint.X, point.X);
            double y = Math.Min(startPoint.Y, point.Y);
            double w = Math.Abs(startPoint.X - point.X);
            double h = Math.Abs(startPoint.Y - point.Y);

            Canvas.SetLeft(SelectionRect, x);
            Canvas.SetTop(SelectionRect, y);
            SelectionRect.Width = w;
            SelectionRect.Height = h;
        }

        private void Cvs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Rect rect = new Rect(Canvas.GetLeft(SelectionRect),Canvas.GetTop(SelectionRect),SelectionRect.RenderSize.Width,SelectionRect.RenderSize.Height);
            foreach (UIElement elem in cvs.Children)
            {
                if (elem == SelectionRect) continue; // 跳过自己

                double left = Canvas.GetLeft(elem);
                double top = Canvas.GetTop(elem);
                double w = elem.RenderSize.Width;
                double h = elem.RenderSize.Height;
                Rect rectTmp = new Rect(left,top,w,h);
                if (rect.IntersectsWith(rectTmp))   // 存在交集
                {

                }
            }
            isSelecting = false;
        }
        #endregion

        #region 边框拖动
        private void RectInit()
        {
            SelectionRect.MouseLeftButtonDown += SelectionRect_MouseLeftButtonDown;
            SelectionRect.MouseMove += SelectionRect_MouseMove;
            SelectionRect.MouseLeftButtonUp += SelectionRect_MouseLeftButtonUp;
        }
        Point oldPoint;
        bool isDragging = false;
        double MinDeltaLength = 0.5;

        private void SelectionRect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isDragging = true;
            oldPoint = e.GetPosition(cvs);
            SelectionRect.CaptureMouse();
            Cursor = Cursors.SizeAll;
            e.Handled = true;
        }

        private void SelectionRect_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isDragging) return;
            Point point = e.GetPosition(cvs);
            Vector vector = point - oldPoint;
            if (vector.Length < MinDeltaLength) return;

            double left = Canvas.GetLeft(SelectionRect);
            double top = Canvas.GetTop(SelectionRect);
            double offsetX = point.X - oldPoint.X;
            double offsetY = point.Y - oldPoint.Y;
            Canvas.SetLeft(SelectionRect,left + offsetX);
            Canvas.SetTop(SelectionRect,top + offsetY);

            oldPoint = point;
        }

        private void SelectionRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDragging = false;
            SelectionRect.ReleaseMouseCapture();
            Cursor = Cursors.Arrow;
        }
        #endregion

        #region 子元素
        private void Border_GotFocus(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("GetFocus");
            ElementDecoratorManager.ShowAll((UIElement)sender);
        }

        private void Border_LostFocus(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("LostFocus");
            ElementDecoratorManager.HideAll((UIElement)sender);
        }
        #endregion

        private void Clear_Button_Click(object sender, RoutedEventArgs e)
        {
            List<UIElement> uIElements = cvs.Children.OfType<UIElement>().ToList();
            foreach (var elem in uIElements)
            {
                ElementDecoratorManager.RemoveAll(elem);
                cvs.Children.Remove(elem);
            }
        }
    }
}
